package cn.edu.hit.core.message;

import com.fasterxml.jackson.databind.JavaType;
import cn.edu.hit.core.Document;

import java.util.*;

/**
 * 对随机的数据进行生成
 */
public class UpDataAdapter extends MessageAdapterHelper2 {
    @Override
    public boolean accept(String type) {
        return "UpData".equalsIgnoreCase(type);
    }

    protected static final Random random = new Random();
    private static JavaType doubleListType = mapper.getTypeFactory().constructParametricType(List.class, Double.TYPE);

//    @Override
//    public Stream<Map> service(Long timestamp) {
//        return configureMap.entrySet().parallelStream().map(entry->{
//            Document result = new Document();
//            String id = entry.getKey();
//            Document configure = (Document) entry.getValue();
//
//            result.put(PORT_ID,id);
//            result.put("portName",configure.getString("portName"));
//            String argString = configure.getString(DATA_SOURCE_ARGS);
//            double val,avg,A;
//
//            List<Double> value;
//            try {
//                value = mapper.readValue(argString,doubleListType);
//                avg = getDefault(value,0,10);
//                A  = getDefault(value,1,20);
//                A = A <0.0000001 ? 1: A;
//            } catch (Exception e) {
//                e.printStackTrace();
//                avg = 10;
//                A = 20;
//            }
//            if(valueMap.containsKey(id)){
//                val = valueMap.get(id);
//                val = generateNextVal(val,avg,A);
//            }else {
//                val = avg;
//            }
//            valueMap.put(id,val);
//            result.put(VALUE,Collections.singletonList(val));
//            result.put(TIMESTAMP,timestamp);
//            return result;
//        });
//    }

    @Override
    protected Object getValue(Object object, String id, Document configure) {
        String argString = configure.getString(DATA_SOURCE_ARGS);
        double val, avg, A;

        List<Double> value;
        try {
            value = mapper.readValue(argString, doubleListType);
            avg = getDefault(value, 0, 10);
            A = getDefault(value, 1, 20);
            A = A < 0.0000001 ? 1 : A;
        } catch (Exception e) {
            e.printStackTrace();
            avg = 10;
            A = 20;
        }
        if (valueMap.containsKey(id)) {
            val = valueMap.get(id);
            val = generateNextVal(val, avg, A);
        } else {
            val = avg;
        }
        valueMap.put(id, val);
        return Collections.singletonList(val);
    }

    protected double generateNextVal(double val, double avg, double A) {
        double r = random.nextDouble();
        double x = 1 / (1 + Math.exp(r)) * Math.signum(r - 0.2) * (A + avg - val);
        return val + x;
    }

    private static double getDefault(List<Double> list, int index, double defaultVal) {
        if (list == null || index < 0 || index >= list.size()) {
            return defaultVal;
        }
        return list.get(index);
    }

    private static Map<String, Double> valueMap = new HashMap<>();
}
